process.env.NODE_ENV = 'development'

const BASE_ADDRESS = '127.0.0.1'


const server = require('../server')


const vite = require('vite')
const connect = require('koa-connect')


const webpackDevConfig = require('../config/webpack.deve.conf')
const colors = require('colors')

const { exec } = require('child_process')

import { localeRunPort } from './server/util/NodeProxyConfig.js'

// 由于相关模板渲染中有使用到entry所以赋值一下 不赋值也可删除模块中使用global的webpackConfig然后通过genEntry获取入口文件
global.webpackConfig = {
  entry: webpackDevConfig.entry
}

/**
 * @description: 启动
 */
vite.createServer({
  root: process.cwd(),
  server: {
    middlewareMode: 'ssr',
    hmr: {
      protocol: 'ws',
      host: BASE_ADDRESS
    },
    proxy: {
      // '/resource': `http://${BASE_ADDRESS}:1024` // 不是最优（相关系统设置模块icon与样式问题） ---目前发现最优（可删除目录下resource文件夹资源走代理）
    },
    watch: {
      usePolling: true,
      interval: 100,
      ignored: ['dist', 'public', 'node_modules', 'script', 'server', 'cli', 'config']
    },
  }
}).then(viteRs => {
  global.vite = viteRs
  server.use(connect(viteRs.middlewares))
  server.init()
  server.listen(localeRunPort, ()=> {
    console.log(`${colors.bgGreen('[本地地址]:') } http://${BASE_ADDRESS}:${localeRunPort}`)
    process.platform === 'darwin' ? exec(`open  http://${BASE_ADDRESS}:${localeRunPort}`) : exec(`start  http://${BASE_ADDRESS}:${localeRunPort}`)
  })
})

